<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Libs\Storage\StorageFile;
use Illuminate\Http\Request;

class LogService
{

    use Service;

    /**
     * 操作日志首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function LogIndexService( $request )
    {
        if ( ! $request->filled('static') ) request()->offsetSet('static', 'logs');
        if ( ! $request->filled('name') ) request()->offsetSet('name', 'mysql');
        $disk                     = $this->getStorage($request->input('static'));
        $log                      = $disk->directories();//获取所有文件夹
        $pic                      = $disk->allFiles($request->input('name'));//获取$request->name下的所有文件
        $res[ 'name' ][ 'count' ] = count($pic);
        $res[ 'pic' ]             = [];
        $res[ 'app' ]             = [];
        foreach ( $pic as $key => $value ) {
            $name    = explode('/', $value);
            $picName = array_slice($name, 0, count($name) - 1);//在数组中根据条件取出一段值
            foreach ( $picName as $k => $v ) $k == 0 ? $res[ 'pic' ][ $key ][ 'name' ] = $v . '/' : $res[ 'pic' ][ $key ][ 'name' ] .= $v . '/';
            $res[ 'pic' ][ $key ][ 'title' ] = end($name);
        }
        foreach ( $log as $key => $value ) {
            $res[ 'app' ][ $key ][ 'value' ] = $value;
            $res[ 'app' ][ $key ][ 'count' ] = count($disk->allFiles($value));
        }
        return view('system.log.index', compact('request', 'res'));
    }


    /**
     * 操作日志详情
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function LogEditService( $request )
    {
        $error  = $this->getErrorCode();
        $static = $request->input('static');
        $title  = $request->input('title');
        if ( ! $static || ! $request->input('name') || ! $title ) return back()->with('error', $error::message(1001));
        $disk = new StorageFile($static);
        $str  = trimAll($title);//去除空格
        if ( ! $disk->exists($str) ) return back()->with('error', $error::message(1016));
        $name = explode('/', $str);
        for ( $i = 0; $i < count($name); $i++ ) if ( $i == count($name) - 1 ) $str = $name[ $i ];
        $res = $disk->get($title);//获取文件url
        return view('system.log.edit', compact('res', 'str', 'request'));
    }
}
